package com.quartz.monitor4job.util;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.quartz.monitor4job.constants.QuartzConstants;

/**
 * 日志工具类
 * @author LIPENG
 *
 * @date 2017年5月5日 上午11:41:38
 *
 * @version V1.0
 */
public class QuartzUtils {
	private static final Logger logger = LoggerFactory.getLogger(QuartzUtils.class);
    /**
     * getHostIp
     * 
     * @return String
     */
    public static String getHostIp() {
        InetAddress netAddress=getInetAddress();
        if (null == netAddress) {
            return null;
        }
        String ip = netAddress.getHostAddress(); // get the ip address
        return ip;
    }
    
    
    public static InetAddress getInetAddress(){  
        try{  
            return InetAddress.getLocalHost();  
        }catch(UnknownHostException e){  
            logger.error("unknown host!",e);
        }  
        return null;  
  
    }  
    /**
     * getHostName
     * 
     * @return String
     */
    public static String getHostName() {
        InetAddress netAddress=getInetAddress();
        if (null == netAddress) {
            return null;
        }
        String name = netAddress.getHostName(); // get the host address
        return name;
    }
    
    /**
     * 把日期转化为日期字符串
     * 
     * @param strDate
     * @param dateFormat
     * @throws ParseException
     */
    public static String getDateStr(Date date) {
        String strDate = "";
        if (date != null) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            strDate = sdf.format(date);
        }
        return strDate;
    }
    
    /**
     * 查询日志保存天数
     * getKeeyDay
     * @return int
     */
    public static int getKeeyDay(){
        try {
//            CodeDict code =CodeDictContext.getContext().getCodeDict(QuartzConstants.QUARTZ_LOG, QuartzConstants.KEEP_DAY);
//            if(code!=null&&StringUtils.isNotBlank(code.getValue())){
//                return Integer.parseInt(code.getValue());
//            }
            return QuartzConstants.DEFAULT_QUARTZ_LOG_KEEP_DAY;
        } catch (Exception e) {
            logger.error("查询日志保存天数出错",e);
            return QuartzConstants.DEFAULT_QUARTZ_LOG_KEEP_DAY;
        }
    }
}